package cn.lijiguo.concurrency.part1.ch06;

import java.util.concurrent.TimeUnit;

/**
 * RightWayStopThreadWithSleepEveryLoop
 * 如果在执行过程中 每次循环都调用 sleep() 或者 wait() 方法，那么 不需要每次迭代都需要检查线程是否已中断
 * @author lijiguo
 * @date 2020/1/6 11:04
 */
public class RightWayStopThreadWithSleepEveryLoop {

    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = () -> {
            int num = 0;
            try {
                while (num <= 100000) {
                    if (num % 100 == 0) {
                        System.out.println(num + " 是 100 的倍数");
                    }
                    num++;

                    Thread.sleep(10);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        Thread t1 = new Thread(runnable, "t1");
        t1.start();

        // 睡眠 100 毫秒
        TimeUnit.SECONDS.sleep(5);

        // 发出中断信号
        t1.interrupt();
    }
}
